#!/bin/sh

alias curl='curl -stderr'

echo "=================================="
#Economy indicator and explanation
curl "http://money.cnn.com/news/economy/index.html" |egrep "The indicator" -A 20 |grep "<td"|cut -d'>' -f3,6,8 |sed -e 's/<\/a>/;/g' -e 's/<\/td/;/g' -e 's/>//g'|cut -d';' -f1,2,3 |awk 'BEGIN { FS = ";" } ; {printf("%20s\t%30s\t%s\n",$1,$2,$3) }'

# new 52-week highs and 52-week lows
export newhighs=`curl "http://stockcharts.com/def/servlet/SC.scan" |grep Highs -A 6 |egrep -o ">[0-9]+<" |sed -e 's/>//g' -e 's/<//g' |tail -n 1`
export newlows=`curl "http://stockcharts.com/def/servlet/SC.scan" |grep Lows -A 6 |egrep -o ">[0-9]+<" |sed -e 's/>//g' -e 's/<//g' |tail -n 1`

#Oil/Gold Price
oilgold=`curl "http://money.cnn.com/data/premarket/index.html" |egrep "Gold" |egrep -o ">[+|-][0-9.]+<" |sed -e 's/>//g' -e 's/<//g' |tr '\n' ' '|awk '{print $1" "$2}'`

#Total Put/Call Ratio
export putcallratio=`curl "http://www.cboe.com/data/mktstat.aspx" |grep "Total Put" -A 1 |tail -n 1 |egrep -o ">[0-9]+.[0-9]+<" |sed -e 's/>//g' -e 's/<//g'`

#Advance/Decline Ratio
export advancedeclineratio=`curl "http://finance.yahoo.com/advances" |egrep -o "\([0-9]+%\)" |tr '\n' ' ' |sed -e 's/(//g' -e 's/)//g' -e 's/%//g' |awk '{print ($1+$2+$3)/($5+$6+$7)}'`

#Bond
export bond=`curl "http://money.cnn.com/markets/bondcenter/index.html" |sed 's/<tr/\n/g' |egrep "[2|5|10|30] yr" |egrep -o "[>|>-][0-9.]+</span"  |cut -d'<' -f1 |tr '\n' ' '`

#try to get http://www.market-harmonics.com/tech_chart_descriptions.htm
echo "=================================="
cat SP500Stocks |while read line
do
    curl -stderr "http://finance.yahoo.com/q/ks?s=$line"  |sed 's/\<td/\n/g' |grep "Forward P/E" -A 3 |egrep -o ">[0-9]+.[0-9]+|N/A" |sed -e 's/>//g' >> tmp$$
done
cat tmp$$ |grep -v 'N/A' > tmp
count=`wc -l tmp |awk '{print $1}'`
cat tmp |tr '\n' '+' |sed 's/+$/\n/g' > tmp$$
allpe=`cat tmp$$ |bc`
avgpe=`echo "scale=2; $allpe/$count" |bc`
echo "SP500 Avg Forward P/E="$avgpe "Cheap if <16.0"

cat SP500Stocks |while read line
do #how many 50MA above 200MA
    curl "http://download.finance.yahoo.com/d/quotes.csv?s=$line&f=m8m6"|sed 's/%//g' |awk 'BEGIN { FS = "," } ; {print($1>$2)}' >> tmp$$
done
over50200=`grep 1 tmp$$ |wc -l`
echo "SP500 50MA over 200MA="$over50200
